<?php

namespace App\Supports\Payment\Channels;

use Exception;
use App\Supports\Payment\Contracts\Channel;
use App\Supports\Payment\Contracts\PayChannel;
use App\Supports\Payment\Contracts\PayBatchChannel;
use App\Supports\Payment\Channels\AliBatch\AlipayBatch;
use Illuminate\Support\Facades\Log;

class AliBatch extends Channel implements PayChannel
{
	public function __construct()
	{
		$this->channelName = 'alibatch';
		$this->setConfig();
		$this->setOrigin();
	}

	public function setConfig()
	{
		$route = cs()->alipay_route;
		$this->config = config("alipay.{$route}");
	}

	public function setOrigin()
	{
		$config = $this->config;
		$this->origin = app(AlipayBatch::class, ['config' => $config]);
	}

	public function getChannelName()
	{
		return $this->channelName;
	}

	public function tools($data = null)
	{
		// TODO: Implement tools() method.
	}

	public function verify($data = null)
	{
		return $this->origin->verify();
	}

	public function find($data = null)
	{
		try {
			return $this->origin->active();
		} catch (Exception $exception) {
			$message = $exception->getCode().$exception->getMessage();
			throw new Exception($message);
		}
	}

	public function pay($data = null)
	{
		// TODO: Implement pay() method.
	}

	public function refund($data = null)
	{
		// TODO: Implement refund() method.
	}

	public function transfer($data = null)
	{
		try {
			return $this->origin->transfer($data);
		} catch (Exception $exception) {
			$message = $exception->getCode().$exception->getMessage();
			throw new Exception($message);
		}
	}

	public function query($data = null)
	{
		try {
			return $this->origin->detail($data);
		} catch (Exception $exception) {
			$message = $exception->getCode().$exception->getMessage();
			throw new Exception($message);
		}
	}
}
